#include <sys_head.h>

void file_copy(int fd_dest,int fd_src);

int main(int argc, char const *argv[])
{
    if (argc != 3)
    {
        printf("格式错误: %s 目标文件 源文件\n",argv[0]);
        return 0;
    }
    //打开文件
    int fd_dest = open(argv[1] , O_CREAT | O_RDWR | O_TRUNC , 0666);
    int fd_src = open(argv[2] , O_RDONLY , 0666);
    if (fd_dest == EOF && fd_src == EOF)
    {
        ERRLOG("open");
        return 0;
    }
    
    file_copy(fd_dest,fd_src);

    //关闭文件
    close(fd_dest);
    close(fd_src);
    return 0;
}

void file_copy(int fd_dest,int fd_src)
{
    char buf[1024];
    memset(buf,0,sizeof(buf));
    while (read(fd_src,buf,sizeof(buf)-1) != 0)
    {
        write(fd_dest,buf,strlen(buf));
        memset(buf,0,sizeof(buf));
    }
    
}